﻿using SuperX.Common.Log;
using SuperX.DataService.Abstract;
using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;

namespace SuperX.DataService
{
    public sealed class NetBytePLCGroup : PLCGroup
    {
        public NetBytePLCGroup(string name, int updateRate, bool active, IPLCDriver plcReader)
        {
            _items = new List<IIOTag>();
            this._updateRate = updateRate;
            this._isActive = active;
            this._plcReader = plcReader;
            this._server = _plcReader.DriverParent;
            this._cacheReader = new NetByteCacheReader();
            this._changedList = new List<int>();
            this.Name = name;
        }

        protected override void Poll()
        {
            if (_items == null || _items.Count == 0) return;
            byte[] cache = (byte[])_cacheReader.Cache;
            int offset = 0;
            try
            {
                foreach (PDUArea area in _rangeList)
                {
                    byte[] rcvBytes = _plcReader.ReadBytes(area.Start, (ushort)area.Len);//从PLC读取数据
                    if (rcvBytes == null)
                    {
                        //_plcReader.Connect();
                        continue;
                    }
                    else
                    {
                        int index = area.StartIndex;//index指向_items中的Tag元数据
                        int count = index + area.Count;
                        while (index < count)
                        {
                            DeviceAddress addr = _items[index].Address;
                            int iByte = addr.CacheIndex;
                            int iByte1 = iByte - offset;
                            if (addr.VarType == DataType.BOOL)
                            {
                                int tmp = rcvBytes[iByte1] ^ cache[iByte];
                                DeviceAddress next = addr;
                                if (tmp != 0)
                                {
                                    while (addr.Start == next.Start)
                                    {
                                        if ((tmp & (1 << next.Bit)) > 0) _changedList.Add(index);
                                        if (++index < count)
                                            next = _items[index].Address;
                                        else
                                            break;
                                    }
                                }
                                else
                                {
                                    while (addr.Start == next.Start && ++index < count)
                                    {
                                        next = _items[index].Address;
                                    }
                                }
                            }
                            else
                            {
                                ushort size = addr.DataSize;
                                for (int i = 0; i < size; i++)
                                {
                                    var rcyItem = rcvBytes[iByte1 + i];
                                    var cacheItem = cache[iByte + i];
                                    var bytLength = iByte1 + i;
                                    if (iByte1 + i < rcvBytes.Length && rcvBytes[iByte1 + i] != cache[iByte + i])
                                    {
                                        _changedList.Add(index);
                                        break;
                                    }
                                }
                                index++;
                            }
                        }
                        for (int j = 0; j < rcvBytes.Length; j++)
                            cache[j + offset] = rcvBytes[j];//将PLC读取的数据写入到CacheReader中
                    }
                    offset += rcvBytes.Length;
                }
            }
            catch (Exception e)
            {
                Logger.Warn($"分组名:{this.FullName}，采集错误，信息：{e.Message}");
            }
        }
    }
}